import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/common/prevent_repeated_event.dart';
import 'package:flutter/material.dart';

mixin SearchDataListMixin<T> on BaseProvider<T> {
  TextEditingController searchController = TextEditingController();
  FocusNode searchFocusNode = FocusNode();
  PreventRepeatedEvent repeatedEvent;

  @override
  void initState() {
    super.initState();
    repeatedEvent = PreventRepeatedEvent();
    repeatedEvent.addEventListener(_handleInput);
    searchController.addListener(_handleSearchInput);
  }

  void _handleSearchInput() {
    update();
    repeatedEvent.sendEvent(searchController.text);
  }

  @override
  void dispose() {
    super.dispose();
    searchController.dispose();
    searchFocusNode.dispose();
    repeatedEvent?.cancel();
  }

  void unFocus() {
    searchFocusNode.unfocus();
  }

  void searchRefreshData(searchData);

  void _handleInput(data) {
    searchRefreshData(data);
  }
}
